 /**
  * EC600S 用户可用串口只有一个 UART2，开发板针对 UART2 提供以下两种使用方式：
  * 1. 使用接口USB2，需外接Micro USB线，开关K1(UART2 SWITCH) 拨到板子丝印CP处
  * 2. 使用接口J6(UART2), 需外接3.3V TTL串口线，开关K1(UART2 SWITCH) 拨到板子丝印JP处, K3 控制 J6 的TX RX接线顺序。
  */
  var uart = require('uart');

  console.log('uart: test start------------------')
 
 /* string convert to decimal array*/
 function StrToDecArray(str) {
     var obj = {
         '0':48,
         '1':49,
         '2':50,
         '3':51,
         '4':52,
         '5':53,
         '6':54,
         '7':55,
         '8':56,
         '9':57,
     }
     return str.split('').map(function(i) {
         if(obj[i]) {
             return obj[i]
         }
         return i.charCodeAt()
     })
 }
 
 /* array convert to  string */
 function ArrayToString(fileData){
   var dataString = "";
   for (var i = 0; i < fileData.length; i++) {
     dataString += String.fromCharCode(fileData[i]);
   }
   return dataString;
 }
 
 /* open uart2 */
 var serial2 = uart.open({
     id: 'serial2',
     success: function() {
         console.log('open uart success')
     },
     fail: function() {
         console.log('open uart fail')
     }
 });
 
 /* msgbuf asc-ii: ABCDabcd++ */
 var msgbuf = [0x41, 0x42, 0x43, 0x44, 0x61, 0x62, 0x63, 0x64, 0x2B, 0X2B];
 
 /* enter "exit" finish uart test */
 var exit = "exit"
 var cnt = 10;
 
 /* uart send only supports array data, not supoort string */
 var intervalHandled = setInterval(function(){
     cnt--;
   if(cnt <= 0){
         clearInterval(intervalHandled);
     }
     serial2.write(msgbuf);
 },2000);
 
 /* uart receive test */
 serial2.on('data', function(data) {
     console.log(ArrayToString(data));
     serial2.write(data);
     if (ArrayToString(data) === exit) {
       console.log('uart2: test finish')
       serial2.close();
     }
 });
 
 